//index.js
//获取应用实例
var app = getApp();
Page({
    data: {
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false, // loading
        swiperCurrent: 0,
        categories: [],
        activeCategoryId: 0,
        goods: [],
        scrollTop: "0",
        loadingMoreHidden: true,   //是否显示下面哥也是有底线的状态值
        searchInput: '',
        p:1,  //分页
        processing:false           //是否正在处理
    },
    onLoad: function () {
        var that = this;

        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        });

        // that.setData({
        //     banners: [
        //         {
        //             "id": 1,
        //             "pic_url": "/images/food.jpg"
        //         },
        //         {
        //             "id": 2,
        //             "pic_url": "/images/food.jpg"
        //         },
        //         {
        //             "id": 3,
        //             "pic_url": "/images/food.jpg"
        //         }
        //     ],
        //     categories: [
        //         {id: 0, name: "全部"},
        //         {id: 1, name: "川菜"},
        //         {id: 2, name: "东北菜"},
        //     ],
        //     activeCategoryId: 0,
			// goods: [
			//                 {
			//                     "id": 1,
			//                     "name": "小鸡炖蘑菇-1",
			//                     "min_price": "15.00",
			//                     "price": "15.00",
			//                     "pic_url": "/images/food.jpg"
			//                 },
			//                 {
			//                     "id": 2,
			//                     "name": "小鸡炖蘑菇-1",
			//                     "min_price": "15.00",
			//                     "price": "15.00",
			//                     "pic_url": "/images/food.jpg"
			//                 },
			//                 {
			//                     "id": 3,
			//                     "name": "小鸡炖蘑菇-1",
			//                     "min_price": "15.00",
			//                     "price": "15.00",
			//                     "pic_url": "/images/food.jpg"
			//                 },
			//                 {
			//                     "id": 4,
			//                     "name": "小鸡炖蘑菇-1",
			//                     "min_price": "15.00",
			//                     "price": "15.00",
			//                     "pic_url": "/images/food.jpg"
			//                 }
        //
			//  ],
        //     loadingMoreHidden: false
        // });
        //调用
        this.getBannerAndCat();
    },
    //每次页面展示的时候 都重新发起请求
    onShow:function () {
      this.getBannerAndCat();
    },
    scroll: function (e) {
        var that = this, scrollTop = that.data.scrollTop;
        that.setData({
            scrollTop: e.detail.scrollTop
        });
    },
    //事件处理函数
    swiperchange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
	listenerSearchInput:function( e ){
	        this.setData({
	            searchInput: e.detail.value
	        });
	 },
	 toSearch:function( e ){
	        this.setData({
	            p:1,
	            goods:[],
	            loadingMoreHidden:true
	        });
	        this.getFoodList();
	},
    tapBanner: function (e) {
        if (e.currentTarget.dataset.id != 0) {
            wx.navigateTo({
                url: "/pages/food/info?id=" + e.currentTarget.dataset.id
            });
        }
    },
    toDetailsTap: function (e) {
        wx.navigateTo({
            url: "/pages/food/info?id=" + e.currentTarget.dataset.id
        });
    },
    //发请求获取轮播图和分类数据
    getBannerAndCat:function () {
        var that = this;
        wx.request({
           url:app.buildUrl("/food/index"),
            //封装头部 头部里面有auth token 从缓存获取的token信息
           header: app.getRequestHeader(),
           success:function (res) {
               var resp = res.data; //数据的返回值
               if(resp.code != 200){
                   app.alert( { "content": resp.msg });
                   return;
               }
               //设置数据
               that.setData({
                   banners:resp.data.banner_list,
                   categories:resp.data.cat_list
               });
               that.getFoodList();
           }
        });
    },
    //在index.wxml分类切换的时候绑定一个点击事件
    catClick:function (e) {
      this.setData({
          //当前激活的id是当前选中的id
          activeCategoryId:e.currentTarget.id,
          //分类查询的时候 需要把一些状态重置
          p:1,
          goods:[],
          loadingMoreHidden: True
      });
        //执行搜索事件传递
       this.getFoodList();
    },
    //上拉实现此方法就可以
    onReachBottom:function () {
      var that = this;
       setTimeout(function () {
           that.getFoodList();
       },500);
    },

    //获取美食列表数据
    getFoodList: function () {
        var that = this;
        if( that.data.processing ){
            return;
        }

        if( !that.data.loadingMoreHidden ){
            return;
        }

        that.setData({
            processing:true
        });

        wx.request({
            url: app.buildUrl("/food/search"),
            header: app.getRequestHeader(),
            data: {
                //当前选中的id
                cat_id: that.data.activeCategoryId,
                //搜索框的内容
                mix_kw: that.data.searchInput,
                p: that.data.p
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                //获取的商品列表数据
                var goods = resp.data.list;
                that.setData({
                    //防止被替换关联
                    goods: that.data.goods.concat( goods ),
                    p: that.data.p + 1,
                    processing:false
                });
                //如果数据没有了 显示哥也是有底线的
                if( resp.data.has_more == 0 ){
                    that.setData({
                        loadingMoreHidden: false
                    });
                }

            }
        });
    }
});
